package com.sxbbc.plat.controller.jz;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.function.common.GetRest;
import com.sxbbc.common.basics.exceptions.ThrowJsonException;
import com.sxbbc.common.basics.rest.RestFactory;
import com.sxbbc.common.basics.rest.RestResponse;
import com.sxbbc.common.basics.utils.calculation.CalcuUtils;
import com.sxbbc.common.basics.utils.date.DateUtil;
import com.sxbbc.common.basics.utils.statics.EnumUtils;
import com.sxbbc.common.basics.utils.statics.StaticUtils;
import com.sxbbc.common.core.entity.jz.*;
import com.sxbbc.common.core.enums.*;
import com.sxbbc.common.core.service.*;
import com.sxbbc.common.core.utils.BasePage;
import com.sxbbc.plat.common.ChickenMapping;
import com.sxbbc.plat.util.SystemLog;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author zhangq
 * @since 2020/3/27
 */
@Controller
public class ChickenController {

    @Autowired
    private IChickenSubscribeService iChickenSubscribeService;
    @Autowired
    private IChickenFundService iChickenFundService;
    @Autowired
    private IChickenLevelService iChickenLevelService;
    @Autowired
    private IChickenCouponService iChickenCouponService;
    @Autowired
    private IChickenOrderService iChickenOrderService;
    @Autowired
    private IChickenLogisticsOrderService iChickenLogisticsOrderService;
    @Autowired
    private ICustomerLevelInfoService iCustomerLevelInfoService;
    @Autowired
    private IChickenBonusService iChickenBonusService;

    @Autowired
    private IChickenBonusConfigureService iChickenBonusConfigureService;

    //region  分红时间配置

    /**
     * 页面-分红时间设置
     *
     * @return org.springframework.web.servlet.ModelAndView
     * @author leehao
     */
    @RequestMapping(value = ChickenMapping.SHOW_CHICKEN_BONUS_DAY)
    public ModelAndView showCustomerLevel() {
        ModelAndView view = new ModelAndView(ChickenMapping.SHOW_CHICKEN_BONUS_DAY);
        return view;
    }

    /**
     * 查询分红时间设置
     *
     * @return com.function.common.RestResponse
     * @author leehao
     */
    @RequestMapping(value = ChickenMapping.SELECT_CHICKEN_BONUS_DAY)
    @ResponseBody
    public RestResponse selectChickenBonusDay(BasePage<ChickenBonusConfigure> page, ChickenBonusConfigure bonusConfigure) {
        QueryWrapper<ChickenBonusConfigure> wrapper = new QueryWrapper<>();
        if (bonusConfigure.getcLevel() == null) {
            bonusConfigure.setcLevel(StaticUtils.FARM_1);
        }
        if (bonusConfigure.getEway() == null) {
            bonusConfigure.setEway(StaticUtils.FARM_EWAY_OWN);
        }
        wrapper.eq("c_level", bonusConfigure.getcLevel())
                .eq("eway", bonusConfigure.getEway());
        iChickenBonusConfigureService.page(page, wrapper);
        return RestFactory.success(page.getRecords(), page.getTotal());
    }

    /**
     * 页面-新增/编辑分红时间设置
     *
     * @param id 会员等级id
     * @return org.springframework.web.servlet.ModelAndView
     * @author leehao
     */
    @RequestMapping(value = ChickenMapping.SHOW_CHICKEN_BONUS_DAY_EDIT)
    public ModelAndView showCustomerLevelEdit(Integer id) {
        QueryWrapper<ChickenBonusConfigure> wrapper = new QueryWrapper<>();
        wrapper.eq("c_level", id);
        List<ChickenBonusConfigure> configures = iChickenBonusConfigureService.list(wrapper);
        ModelAndView view = new ModelAndView(ChickenMapping.SHOW_CHICKEN_BONUS_DAY_EDIT);
        view.addObject("configures", configures);
        return view;
    }

    /**
     * 新增/编辑分红时间设置
     *
     * @return com.function.common.RestResponse
     * @author leehao
     */
    @RequestMapping(value = ChickenMapping.HANDLE_CHICKEN_BONUS_DAY_EDIT)
    @ResponseBody
    @SystemLog(methods = " 新增/编辑分红时间设置", module = "农场管理")
    public RestResponse handleChickenBonusDayEdit(ChickenBonusConfigure bonusConfigure) {
        boolean save = iChickenBonusConfigureService.updateById(bonusConfigure);
        if (!save) {
            throw new ThrowJsonException("操作失败");
        }
        return RestFactory.success("操作成功");
    }

    /**
     * 新增/编辑分红时间设置
     *
     * @return com.function.common.RestResponse
     * @author leehao
     */
    @RequestMapping(value = ChickenMapping.HANDLE_CHICKEN_BONUS_DAY_DELETE)
    @ResponseBody
    @SystemLog(methods = " 删除分红时间设置", module = "农场管理")
    public RestResponse handleChickenBonusDayEdit(Integer id) {
        boolean save = iChickenBonusConfigureService.removeById(id);
        if (!save) {
            throw new ThrowJsonException("操作失败");
        }
        return RestFactory.success("操作成功");
    }

    //endregion

    //region 鸡舍管理

    /**
     * 页面-鸡舍列表页
     *
     * @return
     */
    @RequestMapping(value = ChickenMapping.CR_PAGE_LIST)
    public ModelAndView showChickenRepository() {
        ModelAndView view = new ModelAndView(ChickenMapping.CR_PAGE_LIST);
        return view;
    }

    /**
     * 页面-鸡舍新增页
     *
     * @return
     */
    @RequestMapping(value = ChickenMapping.CR_PAGE_ADD)
    public ModelAndView showChickenRepositoryAdd() {
        ModelAndView view = new ModelAndView(ChickenMapping.CR_PAGE_ADD);
        return view;
    }

    /**
     * 页面-鸡舍编辑页
     *
     * @param id
     * @return
     */
    @RequestMapping(value = ChickenMapping.CR_PAGE_EDIT)
    public ModelAndView showChickenRepositoryEdit(Integer id) {
        ModelAndView view = new ModelAndView(ChickenMapping.CR_PAGE_EDIT);
        ChickenLevel chickenLevel = iChickenLevelService.getById(id);
        view.addObject("detail", chickenLevel);
        return view;
    }

    /**
     * 请求-查询鸡舍信息
     *
     * @param page
     * @param chickenLevel
     * @return
     */
    @RequestMapping(value = ChickenMapping.CR_HANDLE_SELECT)
    @ResponseBody
    public RestResponse handleSelectChickenRepository(BasePage<ChickenLevel> page, ChickenLevel chickenLevel) {
        QueryWrapper<ChickenLevel> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("level");
        if (chickenLevel.getTitle() != null) {
            queryWrapper.like("title", chickenLevel.getTitle());
        }
        iChickenLevelService.page(page, queryWrapper);
        return RestFactory.success(page.getRecords(), page.getTotal());
    }

    @RequestMapping(value = ChickenMapping.CR_HANDLE_ADD)
    @ResponseBody
    public com.function.common.RestResponse handleAddChickenRepository(ChickenLevel chickenLevel) {
        if (chickenLevel.getLevel() != null) {
            return GetRest.getFail("请输入鸡舍等级名");
        }
        if (chickenLevel.getPrice() == null) {
            return GetRest.getFail("请输入鸡苗单价");
        }
        if (chickenLevel.getSalePrice() == null) {
            return GetRest.getFail("请输入卖鸡单价");
        }
        if (chickenLevel.getQty() == null) {
            return GetRest.getFail("请输入总数量");
        }
        if (chickenLevel.getDay() == null) {
            return GetRest.getFail("请输入孵化天数");
        }
        if (chickenLevel.getFraction() == null) {
            return GetRest.getFail("请输入需要创业分");
        }
        boolean saved = iChickenLevelService.save(chickenLevel);
        if (!saved) {
            return GetRest.getFail("保存失败");
        }
        return GetRest.getSuccess("保存成功");
    }

    @RequestMapping(value = ChickenMapping.CR_HANDLE_UPDATE)
    @ResponseBody
    public com.function.common.RestResponse handleEditChickenRepository(ChickenLevel chickenLevel) {
        if (chickenLevel.getQty() != null && chickenLevel.getSalePrice() != null) {
            chickenLevel.setTotalPrice(CalcuUtils.getInstance().mul(new BigDecimal(chickenLevel.getQty()), chickenLevel.getPrice(), 2));
        }
        boolean b = iChickenLevelService.updateById(chickenLevel);
        if (!b) {
            return GetRest.getFail("保存失败");
        }
        return GetRest.getSuccess("保存成功");
    }

    @RequestMapping(value = ChickenMapping.CR_HANDLE_DELETE)
    @ResponseBody
    public com.function.common.RestResponse handleDeleteChickenRepository(Integer id) {
        ChickenLevel repository = new ChickenLevel();
        repository.setId(id);
        repository.setStatus(StaticUtils.STATUS_NO);
        boolean updated = iChickenLevelService.updateById(repository);
        if (!updated) {
            return GetRest.getFail("操作失败");
        }
        return GetRest.getSuccess("操作成功");
    }

    //endregion

    //region 鸡舍订单

    /**
     * 鸡舍列表页面
     *
     * @param crId
     * @param orderType
     * @return
     */
    @RequestMapping(value = ChickenMapping.CO_PAGE_LIST)
    public ModelAndView showChickenOrder(Integer crId, Integer orderType) {
        ModelAndView view = new ModelAndView(ChickenMapping.CO_PAGE_LIST);
        view.addObject("crId", crId);
        view.addObject("orderType", orderType);
        return view;
    }

    /**
     * 鸡舍明细页面
     *
     * @param id
     * @return
     */
    @RequestMapping(value = ChickenMapping.CO_PAGE_DETAIL)
    public ModelAndView showChickenOrder(Integer id) {
        ModelAndView view = new ModelAndView(ChickenMapping.CO_PAGE_DETAIL);
        ChickenOrder chickenOrder = iChickenOrderService.getChickenOrder(id);
        if (chickenOrder != null) {
            chickenOrder.setOrderTypeName(EnumUtils.getEnum(OrderTypeEnums.class, EnumUtils.GET_ID, chickenOrder.getOrderType()).getName());
            chickenOrder.setOrderStatusName(EnumUtils.getEnum(FarmOrderStatusEnums.class, EnumUtils.GET_ID, chickenOrder.getOrderStatus()).getName());
            chickenOrder.setPayTypeName(EnumUtils.getEnum(PayTypeEnums.class, EnumUtils.GET_ID, chickenOrder.getPayType()).getName());
        }
        view.addObject("detail", chickenOrder);
        return view;
    }

    /**
     * 鸡舍列表查询
     *
     * @param page
     * @param chickenOrder
     * @return
     */
    @RequestMapping(value = ChickenMapping.CO_HANDLE_SELECT)
    @ResponseBody
    public RestResponse CO_HANDLE_SELECT(BasePage<ChickenOrder> page, ChickenOrder chickenOrder) {
        if (org.apache.commons.lang3.StringUtils.isNotBlank(chickenOrder.getQueryPayTime())) {
            String[] times = chickenOrder.getQueryPayTime().split("~");
            chickenOrder.setQueryStartPayTime(times[0]);
            chickenOrder.setQueryEndPayTime(times[1]);
        }
        iChickenOrderService.selectChickenOrderList(page, chickenOrder);
        return RestFactory.success(page.getRecords(), page.getTotal());
    }

    //endregion

    //region 提鸡券管理

    @RequestMapping(value = ChickenMapping.CC_PAGE_LIST)
    public ModelAndView showChickenCoupon(ChickenCoupon chickenCoupon) {
        ModelAndView view = new ModelAndView(ChickenMapping.CC_PAGE_LIST);
        List<ChickenCouponStatusEnums> statusList = ChickenCouponStatusEnums.queryList();
        view.addObject("orderCode", chickenCoupon.getOrderCode());
        view.addObject("statusList", statusList);
        return view;
    }

    //    @RequestMapping(value = ChickenMapping.CC_PAGE_DETAIL)
    //    public ModelAndView showChickenCoupon(Integer id) {
    //        ModelAndView view = new ModelAndView(ChickenMapping.CC_PAGE_DETAIL);
    //        ChickenOrder chickenOrder = iChickenOrderService.getChickenOrder(id);
    //        if (chickenOrder!=null) {
    //            chickenOrder.setOrderTypeName(EnumUtils.getEnum(OrderTypeEnums.class, EnumUtils.GET_ID, chickenOrder.getOrderType()).getName());
    //            chickenOrder.setOrderStatusName(EnumUtils.getEnum(ChickenCouponStatusEnums.class, EnumUtils.GET_ID, chickenOrder.get()).getName());
    //            chickenOrder.setPayTypeName(EnumUtils.getEnum(PayTypeEnums.class, EnumUtils.GET_ID, chickenOrder.getPayType()).getName());
    //        }
    //        view.addObject("detail",chickenOrder);
    //        return view;
    //    }

    /**
     * 请求-查询提鸡券信息
     *
     * @param page
     * @param chickenCoupon
     * @return
     */
    @RequestMapping(value = ChickenMapping.CC_HANDLE_SELECT)
    @ResponseBody
    public RestResponse selectChickenCouponList(BasePage<ChickenCoupon> page, ChickenCoupon chickenCoupon) {
        iChickenCouponService.selectChickenCouponList(page, chickenCoupon);
        return RestFactory.success(page.getRecords(), page.getTotal());
    }

    //endregion

    //region 预约提鸡

    /**
     * 页面-鸡舍列表页
     *
     * @return
     */
    @RequestMapping(value = ChickenMapping.CS_PAGE_LIST)
    public ModelAndView showChickenSubscribeOrder() {
        ModelAndView view = new ModelAndView(ChickenMapping.CS_PAGE_LIST);
        // 订单状态
        List<ChickenSubscribeStatusEnums> statusList = ChickenSubscribeStatusEnums.queryList();
        view.addObject("statusList", statusList);
        return view;
    }

    /**
     * 查询预约提鸡列表
     *
     * @param page
     * @return
     */
    @RequestMapping(value = ChickenMapping.CS_HANDLE_SELECT)
    @ResponseBody
    public RestResponse selectChickenSubscribeList(BasePage<ChickenSubscribe> page, ChickenSubscribe chickenSubscribe) {
        iChickenSubscribeService.selectChickenSubscribeList(page, chickenSubscribe);
        return RestFactory.success(page.getRecords(), page.getTotal());
    }

    //endregion

    //region 物流提鸡

    /**
     * 页面-物流提鸡列表页
     *
     * @return
     */
    @RequestMapping(value = ChickenMapping.CL_PAGE_LIST)
    public ModelAndView showChickenLogisticsOrder() {
        ModelAndView view = new ModelAndView(ChickenMapping.CL_PAGE_LIST);
        return view;
    }
    //endregion

    //region  基金

    /**
     * 鸡舍基金列表页面
     *
     * @param orderCode
     * @return
     */
    @RequestMapping(value = ChickenMapping.CF_PAGE_LIST)
    public ModelAndView showChickenFund(String orderCode) {
        ModelAndView view = new ModelAndView(ChickenMapping.CF_PAGE_LIST);
        List<CustomerLevelInfo> list = iCustomerLevelInfoService.list();
        HashMap<String, Object> hashMap = iChickenFundService.queryTotalFundByDate(null);
        view.addObject("levelList", list);
        view.addObject("totalMoney", hashMap.get("totalMoney"));
        view.addObject("queryTmie", hashMap.get("queryTmie"));
        view.addObject("orderCode", orderCode);
        return view;
    }

    @RequestMapping(value = ChickenMapping.CF_HANDLE_SELECT_TOTALMONEY)
    @ResponseBody
    public RestResponse CO_HANDLE_SELECT(String queryTime) {
        HashMap<String, Object> hashMap = iChickenFundService.queryTotalFundByDate(queryTime);
        return RestFactory.success(hashMap);
    }

    /**
     * 鸡舍基金列表查询
     *
     * @param page
     * @param chickenFund
     * @return
     */
    @RequestMapping(value = ChickenMapping.CF_HANDLE_SELECT)
    @ResponseBody
    public RestResponse CO_HANDLE_SELECT(BasePage<ChickenFund> page, ChickenFund chickenFund) {
        if (org.apache.commons.lang3.StringUtils.isNotBlank(chickenFund.getQueryTime())) {
            String[] times = chickenFund.getQueryTime().split("~");
            chickenFund.setStartTime(times[0]);
            chickenFund.setEndTime(times[1]);
        }
        iChickenFundService.selectChickenFundList(page, chickenFund);
        return RestFactory.success(page.getRecords(), page.getTotal());
    }

    /**
     * 批量延迟分红时间
     *
     * @param ids
     * @param day
     * @return
     */
    @RequestMapping(value = ChickenMapping.CF_HANDLE_LATER_DAY)
    @ResponseBody
    public RestResponse handleLaterDay(@RequestParam(value = "ids[]") List<Integer> ids, int day) {
        iChickenFundService.handleLaterDay(ids, day);
        return RestFactory.success("成功");
    }

    //endregion

    //region 分红预发放

    /**
     * 鸡舍分红预发放记录
     *
     * @param
     * @return
     */
    @RequestMapping(value = ChickenMapping.CB_PAGE_LIST)
    public ModelAndView showchickenBonusList() {
        ModelAndView view = new ModelAndView(ChickenMapping.CB_PAGE_LIST);
        List<CustomerLevelInfo> list = iCustomerLevelInfoService.list();
        HashMap<String, Object> hashMap = iChickenBonusService.queryTotalFundByDate(null);
        view.addObject("totalMoney", hashMap.get("totalMoney"));
        view.addObject("queryTmie", hashMap.get("queryTmie"));
        view.addObject("levelList", list);
        return view;
    }

    /**
     * 鸡舍分红预发放记录
     *
     * @param
     * @return
     */
    @RequestMapping(value = ChickenMapping.CB_HANDLE_LIST)
    @ResponseBody
    public RestResponse querychickenBonusList(BasePage<ChickenBonus> page, ChickenBonus chickenBonus) {
        if (org.apache.commons.lang3.StringUtils.isNotBlank(chickenBonus.getQueryTime())) {
            String[] times = chickenBonus.getQueryTime().split("~");
            chickenBonus.setStartTime(times[0]);
            chickenBonus.setEndTime(times[1]);
        }
        iChickenBonusService.querychickenBonusList(page, chickenBonus);
        return RestFactory.success(page.getRecords(), page.getTotal());
    }

    /**
     * 设置分红
     *
     * @param
     * @return
     */
    @RequestMapping(value = ChickenMapping.CB_HANDLE_SETTINGBONUS)
    @ResponseBody
    public RestResponse querychickenBonusList(ChickenBonus chickenBonus) {
        ChickenBonus bonus = new ChickenBonus();
        if (chickenBonus.getId() == null) {
            return RestFactory.fail("参数缺少");
        }

        ChickenBonus chickenBonus1 = iChickenBonusService.getById(chickenBonus.getId());
        if (chickenBonus1 != null && CalcuUtils.getInstance().isEquals(chickenBonus1.getStatus(), StaticUtils.STATUS_YES)) {
            return RestFactory.fail("该分红已发放，不可进行修改!");
        }

        bonus.setId(chickenBonus.getId());
        if (chickenBonus.getChickenNum() == null && chickenBonus.getCouponNum() == null) {
            if (StringUtils.isBlank(chickenBonus.getBonusTimeStr())) {
                return RestFactory.fail("请选择一个分红时间");
            } else {
                Date date = DateUtil.getInstance().toDate(chickenBonus.getBonusTimeStr());
                bonus.setBonusTime(date);
            }
        } else {
            bonus.setChickenNum(chickenBonus.getChickenNum());
            bonus.setCouponNum(chickenBonus.getCouponNum());
        }

        iChickenBonusService.updateById(bonus);
        return RestFactory.success("操作成功");
    }

    /**
     * 查看某个时间段预分红总金额
     *
     * @param queryTime
     * @return
     */
    @RequestMapping(value = ChickenMapping.CF_HANDLE_SELECT_BONUS_TOTALMONEY)
    @ResponseBody
    public RestResponse handleSelectBonusTotalMoney(String queryTime) {
        HashMap<String, Object> hashMap = iChickenBonusService.queryTotalFundByDate(queryTime);
        return RestFactory.success(hashMap);
    }

    @RequestMapping(value = ChickenMapping.CF_HANDLE_EXECUTE)
    @ResponseBody
    public RestResponse handleBonusExecute(Integer id) {
        return iChickenBonusService.handleBonusExecute(id);
    }

    //endregion
}
